package in.muton.edushare.pages.group;

import in.muton.edushare.EduShareSession;
import in.muton.edushare.auth.roles.PageRoleCheck;
import in.muton.edushare.components.images.ProfileImage;
import in.muton.edushare.pages.EduShareBasePage;
import in.muton.edushare.url.EduShareURLPanel;
import in.muton.edushare.url.EntityPageParams;
import in.muton.edushare.url.URLLoader;
import in.muton.edushare.wicket.models.CayenneModel;

import org.apache.wicket.PageParameters;
import org.apache.wicket.RestartResponseException;
import org.apache.wicket.behavior.AttributeAppender;
import org.apache.wicket.markup.html.CSSPackageResource;
import org.apache.wicket.markup.html.WebMarkupContainer;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.link.BookmarkablePageLink;
import org.apache.wicket.model.Model;
import org.apache.wicket.model.PropertyModel;

import com.edushare.common.constants.ApplicationConstants;
import com.edushare.common.constants.URLConstants;
import com.edushare.common.dao.GroupsDAO;
import com.edushare.common.vo.Groups;
import com.edushare.common.vo.Universities;

public abstract class GroupBase extends EduShareBasePage {

	protected CayenneModel<Groups> groupModel;
	protected static String[] menuItems = { "groupNews", "groupCalendar","folders",
											"groupPeople", "groupInvite", "groupUniv",
											"editProfile", "groupPoll" };
	
	public GroupBase(PageParameters params) {
		int groupId = params.getAsInteger("0", 0);
		Groups g = null;
		if(groupId == 0 || (g = new GroupsDAO(getSessionORMInterface()).getGroupById(groupId)) == null){
			throw new RestartResponseException(URLLoader.getPageClass(URLConstants.PAGE_NOT_FOUND_PATH));
		}
		this.setGroupModel(new CayenneModel<Groups>(g));
		prc = new PageRoleCheck(g, getLoggedInUser());
		if(!EduShareSession.get().isLoggedIn() || !getLoggedInUser().isGroupMember(g)){
//			throw new RestartResponseException(URLLoader.getPageClass(URLConstants.UNAUTHORIZED_PATH));
			throw new RestartResponseException(URLLoader.getPageClass(URLConstants.GROUPS_PROFILE), EntityPageParams.getParams(groupModel));
		}
		add(CSSPackageResource.getHeaderContribution(GroupBase.class, "GroupBase.css"));
		add(new BookmarkablePageLink<Void>("vwProfile", URLLoader.getPageClass(URLConstants.GROUPS_PROFILE), EntityPageParams.getParams(groupModel)));
		intialize();
		loadMenu();
	}

	private void loadMenu() {
		String highlight = getActivePage();
		for (String s : menuItems) {
			WebMarkupContainer wmc;
			add(wmc = new WebMarkupContainer(s));
			if (highlight.equals(s)) {
				wmc.add(new AttributeAppender("class", new Model<String>(
						"es-selected"), " "));
			}
			
			if (s.equals("groupNews")) {
				wmc.add(new BookmarkablePageLink<Void>("link", GroupNews.class,EntityPageParams.getParams(getGroupModel())));
			} else if (s.equals("groupCalendar")) {
				wmc.add(new BookmarkablePageLink<Void>("link", GroupCalendar.class,EntityPageParams.getParams(getGroupModel())));
			}else if (s.equals("folders")) {
				wmc.add(new BookmarkablePageLink<Void>("link", GroupFolder.class,EntityPageParams.getParams(getGroupModel())));
			}else if (s.equals("groupPeople")) {
				wmc.add(new BookmarkablePageLink<Void>("link", GroupPeople.class,EntityPageParams.getParams(getGroupModel())));
			}else if (s.equals("groupInvite")) {
				wmc.add(new BookmarkablePageLink<Void>("link", InvitePeople.class,EntityPageParams.getParams(getGroupModel())));
				if(!getLoggedInUser().isGroupAdmin(this.groupModel.getObject())){
					wmc.setVisible(false);
				}
			}else if (s.equals("groupUniv")) {
				wmc.add(new BookmarkablePageLink<Void>(
														"link", 
														URLLoader.getPageClass(URLConstants.UNIVERSITY_PROFILE_PATH),
														EntityPageParams.getParams(new CayenneModel<Universities>
																		(getGroupModel().getObject().getUniversity())))
													);
				
			}else if (s.equals("editProfile")) {
				wmc.add(new BookmarkablePageLink<Void>("link", EditProfile.class,EntityPageParams.getParams(getGroupModel())));
				if(!getLoggedInUser().isGroupAdmin(this.groupModel.getObject())){
					wmc.setVisible(false);
				}
			}else if (s.equals("groupPoll")) {
				wmc.add(new BookmarkablePageLink<Void>("link", GroupPoll.class,EntityPageParams.getParams(getGroupModel())));
			}
		}
	}

	protected abstract String getActivePage();

	private void intialize() {
		
		add(new Label("groupName", new PropertyModel<String>(getGroupModel(), "groupName")));
		add(new ProfileImage("groupIcon", getGroupModel(), ApplicationConstants.FILE_SUFFIX_ICON));
		add(new EduShareURLPanel("univLink", new CayenneModel<Universities>(getGroupModel().getObject().getUniversity())));
		
	}
	
	@Override
	public String getUserRole() {
		String str;
		if(EduShareSession.get().isLoggedIn()){
			str = getLoggedInUser().isGroupAdmin(getGroupModel().getObject())? " : Group Admin": " : Group Member";
		} 
		else {
			str = "";
		}
		
		return str;
	}

	public void setGroupModel(CayenneModel<Groups> groupModel) {
		this.groupModel = groupModel;
	}

	public CayenneModel<Groups> getGroupModel() {
		return groupModel;
	}

}
